package com.easytouch.core.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * 文件相关操作工具类
 * Created by gulin on 2017/7/26.
 */

public class FileTools {

    //设置售货机编号到文件中
    public static String setMac2File(final String mac)
    {
        String result = "00000000";
        BufferedReader br = null;
        BufferedWriter bw = null;
        try
        {
            File file = new File(SDCardTools.getSDCardPath()+"/mac.txt");
            if(file.exists())
            {
                br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                result = br.readLine();
                if(mac.equals(result))
                {
                    return result;
                }
                else
                {
                    if("00000000".equals(result))
                    {
                        result = mac;
                    }
                    else
                    {
                        if(!mac.equals("00000000"))
                        {
                            result = mac;
                        }
                    }
                }
            }
            else
            {
                file.createNewFile();
                result = mac;
            }
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
            bw.write(result);
            bw.flush();
        }catch(IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(br != null)
            {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bw != null)
            {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    //从文件中获取售货机编号
    public static String getMacFromFile()
    {
        String result = null;
        BufferedReader br = null;
        try
        {
            File file = new File(SDCardTools.getSDCardPath()+"/mac.txt");
            if(file.exists())
            {
                br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                result = br.readLine();
                if(result != null && result.length() == 8 && !result.equals("00000000"))
                {
                    return result;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }catch(IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(br != null)
            {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 拷贝文件
     * @param fileSrc 原文件
     * @param fileDes 目标文件
     * @return 是否成功
     */
    public static boolean copyFile(final String fileSrc, final String fileDes)
    {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        try {
            // 新建文件输入流并对它进行缓冲
            inBuff = new BufferedInputStream(new FileInputStream(fileSrc));
            // 新建文件输出流并对它进行缓冲
            outBuff = new BufferedOutputStream(new FileOutputStream(fileDes));

            // 缓冲数组
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            // 刷新此缓冲的输出流
            outBuff.flush();
        }
        catch(IOException e)
        {
            return false;
        }
        finally
        {
            try
            {
                // 关闭流
                if (inBuff != null)
                    inBuff.close();
                if (outBuff != null)
                    outBuff.close();
            } catch (IOException e) {
                return false;
            }
        }
        return true;
    }
}
